package com.xiaoyu.lanling.share

import `in`.srain.cube.util.CheckUtil
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.net.Uri
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.xiaoyu.base.AppConfig
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.app.GlobalUI
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.util.AppUtil

object ShareHelper {

    const val PKG_WECHAT = "com.tencent.mm"
    const val COMP_WECHAT = "com.tencent.mm.ui.tools.ShareImgUI"
    const val COMP_TIMELINE = "com.tencent.mm.ui.tools.ShareToTimeLineUI"
    const val PKG_QQ = "com.tencent.mobileqq"
    const val COMP_QQ = "com.tencent.mobileqq.activity.JumpActivity"
    const val PKG_MOMO = "com.immomo.momo"
    const val PKG_KUAISHOU = "com.smile.gifmaker"
    const val PKG_TIKTOK = "com.ss.android.ugc.aweme"

    val wechatComponent by lazy { ComponentName(PKG_WECHAT, COMP_WECHAT) }
    val timelineComponent by lazy { ComponentName(PKG_WECHAT, COMP_TIMELINE) }
    val qqComponent by lazy { ComponentName(PKG_QQ, COMP_QQ) }

    val mIWXAPI by lazy {
        WXAPIFactory.createWXAPI(AppContext.getContext(), AppConfig.wechatAppId(), true).also {
            it.registerApp(AppConfig.wechatAppId())
        }
    }

    fun capableOfShare(context: Context, pkgName: String): Boolean {
        if (!AppUtil.isAppInstalled(context, pkgName))
            return false
        return true
    }

    fun shareQQ(context: Context, text: String) {
        val intent = Intent()
        intent.component = qqComponent
        intent.action = Intent.ACTION_SEND
        intent.putExtra(Intent.EXTRA_STREAM, Uri.EMPTY);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setType("image/*");
        intent.putExtra("summary", text);

        if (CheckUtil.checkAppsIsExist(context, intent)) {
            context.startActivity(intent)
        } else {
            GlobalUI.getInstance().showToast(R.string.invite_share_qq_not_exist_toast)
        }
    }

    fun launchMoMo(context: Context) {
        val launchIntentForPackage = context.packageManager.getLaunchIntentForPackage(PKG_MOMO)
        if (CheckUtil.checkAppsIsExist(context, launchIntentForPackage)) {
            context.startActivity(launchIntentForPackage)
        }
    }

    fun launchKuaishou(context: Context) {
        val launchIntentForPackage = context.packageManager.getLaunchIntentForPackage(PKG_KUAISHOU)
        if (CheckUtil.checkAppsIsExist(context, launchIntentForPackage)) {
            context.startActivity(launchIntentForPackage)
        }
    }

    fun launchTiktok(context: Context) {
        val launchIntentForPackage = context.packageManager.getLaunchIntentForPackage(PKG_TIKTOK)
        if (CheckUtil.checkAppsIsExist(context, launchIntentForPackage)) {
            context.startActivity(launchIntentForPackage)
        }
    }
}